# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: user_test.py
# Time: 9:32

from common.utils.base_util import ResultBase
from common.utils.file_util import get_data_json
from common.utils.http_util import RestClient
from common.utils.read_util import ReadFileData


class UserRequests():
    def __init__(self, log, ip):
        self.ip = ip
        self.__log = log
        self.re = RestClient(log, ip)
        self.read = ReadFileData().load_json(get_data_json("api_url.json"))
        self.head = ReadFileData().load_json(get_data_json("user_header.json"))

    def get_access_to_all_students(self, username):
        '''
        获取的单个学生信息
        :return: 自定义的关键字返回结果 result
        '''
        result = ResultBase()
        params = {"stu_name": f"{username}"}
        res = self.re.get(self.read.get("access_to_all_students"), params=params)
        result.success = False
        result.response = res
        if res.json()["error_code"] == 0:
            result.success = True
            result.msg = res.json()["stu_info"]
        else:
            result.error = "查询用户 ==>> 接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["error_code"], res.json()["msg"])
            result.msg = res.json()["msg"]
        self.__log.info("获取某个学生信息 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def get_login_students(self, username, password):
        '''
        学生登录
        :param username: 账号
        :param password: 密码
        :return: 自定义的关键字返回结果 result
        '''
        result = ResultBase()
        data = {
            "username": username,
            "passwd": password
        }
        res = self.re.post(self.read.get("login_students"), data=data)
        result.success = False
        if res.json()["error_code"] == 0:
            result.success = True
            result.sigin = res.json()["login_info"]["sign"]
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["error_code"], res.json()["msg"])
        # result.msg = res.json()["stu_info"]
        result.response = res
        self.__log.info("用户登录 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def get_add_student_information(self, name, grade, phone, sex=None, age=None, addr=None):
        '''
        添加学生信息
        :param name: 学生姓名
        :param grade: 班级
        :param phone: 电话
        :param sex: 性别，如果不传默认为男
        :param age: 年龄，如果不传默认为18
        :param addr: 地址，如果不传默认为北京市昌平区
        :return: 自定义的关键字返回结果 result
        '''
        result = ResultBase()
        json_data = {
            "name": name,
            "grade": grade,
            "phone": phone,
            "sex": sex,
            "age": age,
            "addr": addr
        }
        header = self.head.get("user_headers")
        res = self.re.post(self.read.get("add_student_information"), json=json_data, headers=header)
        result.success = False
        if res.json()["error_code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["error_code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        self.__log.info("注册用户 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def get_student_gold_coin_recharg(self, stu_id, gold, username, sigin):
        '''
        添加金币充值
        :param stu_id: 学生ID
        :param gold: 金币数量
        :param username: 用户名
        :param sigin: 用户sigin
        :return: 自定义的关键字返回结果 result
        '''
        result = ResultBase()
        data = {

            "stu_id": stu_id,
            "gold": gold
        }
        cookie = {
            username: sigin
        }
        res = self.re.post(self.read.get("student_gold_coin_recharg"), data=data, cookies=cookie)
        result.success = False
        if res.json()["error_code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["error_code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        self.__log.info("添加金币充值 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def get_access_all_student_information(self):
        '''
        获取所有学生信息
        :return: 自定义的关键字返回结果 result
        '''
        result = ResultBase()
        header = self.head.get("get_all_the_information_headers")
        res = self.re.get(self.read.get("access_all_student_information"), headers=header)
        result.success = False
        if res.json()["error_code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["error_code"], res.json()["stu_info"])
        result.msg = res.json()["stu_info"]
        result.response = res
        self.__log.info("获取所有学生信息 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result

    def get_files_upload(self, files):
        '''
        文件上传
        :param files: 文件路径
        :return: 自定义的关键字返回结果 result
        '''
        result = ResultBase()
        file = {"file": files}
        res = self.re.post(self.read.get("files_upload"), files=file)
        result.success = False
        if res.json()["error_code"] == 0:
            result.success = True
        else:
            result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["error_code"], res.json()["msg"])
        result.msg = res.json()["msg"]
        result.response = res
        self.__log.info("文件上传 ==>> 返回结果 ==>> {}".format(result.response.text))
        return result


if __name__ == '__main__':
    pass
    # from common.utils.log_util import logger

    # ip = 'http://api.nnzhp.cn'
    # UserRequests(logger(), ip).get_student_gold_coin_recharg("4774", "10000", "niuhanyang",
    #                                                          "859d9175afd872494dd8e783c2881892")
    # UserRequests(logger(),ip).get_login_students("niuhanyang","aA123456")
